﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace APRIL
{
    class ValueHelper
    {
        #region 获取整数的某一位，设置整数的某一位
        /// <summary>
        /// 取整数的某一位
        /// </summary>
        /// <param name="_Resource">要取某一位的整数</param>
        /// <param name="_Mask">要取的位置索引，自右至左为0-7</param>
        /// <returns>返回某一位的值（0或者1）</returns>
        public static int getIntegerSomeBit(int _Resource, int _Mask)
        {
            return _Resource >> _Mask & 1;
        }


        /// <summary>
        /// 将整数的某位置为0或1
        /// </summary>
        /// <param name="_Mask">整数的某位</param>
        /// <param name="a">整数</param>
        /// <param name="flag">是否置1，TURE表示置1，FALSE表示置0</param>
        /// <returns>返回修改过的值</returns>
        public static int setIntegerSomeBit(int _Mask, int a, bool flag)
        {
            if (flag)
            {
                a |= (0x1 << _Mask);
            }
            else
            {
                a &= ~(0x1 << _Mask);
            }
            return a;
        }
        #endregion

        /// <summary>
        /// 转换为二进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToErJin(int value)
        {
            int temp = 0;

            int shang = 1;

            int yushu;

            while (shang != 0)
            {

                shang = (int)value / 2;

                yushu = value % 2;

                value = shang;

                temp += yushu;

                if (shang != 0)
                {

                    temp = temp * 10;

                }

            }

            //最后将 temp 倒序

            string tempStr = temp.ToString();

            int tempLength = tempStr.Length;

            string resultStr = string.Empty;

            for (int i = 0; i < tempLength; i++)
            {

                resultStr = tempStr[i] + resultStr;

            }

            return int.Parse(resultStr);

        }



        /// <summary>
        /// 转换为十进制（主要算法：个位数 * 2的零次方 + 十位数 * 2的一次方 + 百位数 * 2的二次方 + ...）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToShijin2(int value)
        {

            int temp = 0;

            int shang = value;

            int yushu;

            int mark = 0;

            while (shang != 0)
            {

                yushu = shang % 10;

                shang = shang / 10;

                temp += yushu * (int)Math.Pow(2, mark);

                mark++;

            }

            return temp;

        }


        /// <summary>
        /// 二进制转十进制的另一种方法（主要算法：1110111 = 1 * 2~6 + 1 * 2~5 + 1 * 2~4 + 0 * 2~3 + 1 * 2~2 + 1 * 2~1 + 1 * 2~0）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int ToShijin(int value)
        {
            string strValue = value.ToString();

            int valueLength = strValue.Length;

            int result = 0;

            for (int i = 0; i < valueLength; i++)
            {

                result += int.Parse(strValue[i].ToString()) * (int)Math.Pow(2, valueLength - 1 - i);

            }

            return result;
        }

        public static string Hex2Ten(string hex)
        {
            int ten = 0;
            for (int i = 0, j = hex.Length - 1; i < hex.Length; i++)
            {
                ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j));
                j--;
            }
            return ten.ToString();
        }

        public static int HexChar2Value(string hexChar)
        {
            switch (hexChar)
            {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    return Convert.ToInt32(hexChar);
                case "a":
                case "A":
                    return 10;
                case "b":
                case "B":
                    return 11;
                case "c":
                case "C":
                    return 12;
                case "d":
                case "D":
                    return 13;
                case "e":
                case "E":
                    return 14;
                case "f":
                case "F":
                    return 15;
                default:
                    return 0;
            }
        }

        /// <summary>
        ///十进制转二制  
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string DtoB(int d)
        {
            string b = "";
            //判断该数如果小于2，则直接输出 
            if (d < 2)
            {
                b = d.ToString();
            }
            else
            {
                int c;
                int s = 0;
                int n = d;
                while (n >= 2)
                {
                    s++;
                    n = n / 2;
                }
                int[] m = new int[s];
                int i = 0;
                do
                {
                    c = d / 2;
                    m[i++] = d % 2;
                    d = c;
                } while (c >= 2);
                b = d.ToString();
                for (int j = m.Length - 1; j >= 0; j--)
                {
                    b += m[j].ToString();
                }
            }
            return b;
        } 
    }
}
